package edu.chinasoft.servlet;

import edu.chinasoft.domain.Dept;
import edu.chinasoft.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 
 * @author 
 * @date 2020年1月16日:下午8:22:35
 * @version V1.0
 * @Description: 
 * 
*/
@WebServlet(name="AddDeptServlet",value="/servlet/adddeptservlet")
public class  AddDeptServlet  extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(AddDeptServlet.class);
	private DeptService deptService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" AddDeptServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		logger.debug(" AddDeptServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" AddDeptServlet >doPost start.......");
		String dept_name = request.getParameter("dept_name");
		String dept_addr = request.getParameter("dept_addr");
		Dept dept = new Dept();
		dept.setDeptName(dept_name);
		dept.setDeptAddr(dept_addr);

		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		DeptService deptServiceImp = wac.getBean("deptService", DeptService.class);
		this.deptService = deptServiceImp;

		Boolean result = deptService.insertDept(dept);
		if(result!=null) {
			response.sendRedirect(""+request.getContextPath()+"/servlet/showdeptservlet");
		}else {
			response.sendRedirect(""+request.getContextPath()+"/adddepterror.jsp");
		}
		
		logger.debug(" AddDeptServlet >doPost end.......");
	}
	
	

	
}